var app = getApp();
Component({
  options: {
    multipleSlots: true,
    addGlobalClass: true,
  },
  data: {
    str: ''
  },
  properties: {
    class: String,
    time: {
      type: [Number,String,Object],
      value: null
    },
    format: {
      type: String,
      value: 'y/m/d'
    },
    short:{
      type:Boolean,
      value:false
    },
    isShowYear: {
      type:Boolean,
      value: true
    },
    second:{
      type:Number,
      value:0
    }
  },
  observers: {
    'time': function(time) {
      this.formated();
    }
  },
  ready: function() {
    this.formated();
  },
  methods: {
    formated() {
      this.data.time <= 10000000000 && (this.data.time = this.data.time * 1000)
      var format = this.data.format,
        short = this.data.short,
        type = Object.prototype.toString.apply(this.data.time),
        time;
      
      if(/(string|number)/ig.test(type)){
        time = new Date(this.data.time);
      }else if(/(date)/ig.test(type)){
        time = this.data.time
      }else{
        time = new Date()
      }

      if(this.data.second){
        time.setSeconds(+(this.data.second+time.getSeconds()));
      }

      format = this.data.isShowYear ? format.replace("y", time.getFullYear()) : format.replace("y/", '');
      format = format.replace("m", (time.getMonth() + 1) < 10 ? (short?'':'0') + (time.getMonth() + 1) : (time.getMonth() + 1));
      format = format.replace("d", time.getDate() < 10 ? (short?'':'0') + time.getDate() : time.getDate());
      format = format.replace("h", time.getHours() < 10 ? (short?'':'0') + time.getHours() : time.getHours());
      format = format.replace("i", time.getMinutes() < 10 ? (short?'':'0') + time.getMinutes() : time.getMinutes());
      format = format.replace("s", time.getSeconds() < 10 ? (short?'':'0') + time.getSeconds() : time.getSeconds());
      this.setData({
        str: format
      })
    }
  }
})